import { Component } from '@angular/core';
import { IonicPage, NavController, ToastController, NavParams, LoadingController, AlertController } from 'ionic-angular';
import { ExceptionReportApi } from '../../api/exception-report/ExceptionReportApi';

@IonicPage()
@Component({
  selector: 'page-exception-report',
  templateUrl: 'exception-report.html'
})
export class ExceptionReport {
  private planId: string;
  private planPositionId: string;
  private change: boolean;
  private exceptionInfo: string;
  private planCode: string;
  private addressName: string;
  private info: string;

  constructor(private alertCtrl: AlertController, private loadingCtrl: LoadingController, private params: NavParams, public navCtrl: NavController, private toastCtrl: ToastController, private exceptionReportApi: ExceptionReportApi) {
    this.planId = params.get("planId");
    this.planPositionId = params.get("planPositionId");
    this.change = true;
    this.exceptionInfo = "";
    this.planCode = "";
    this.addressName = "";
    this.info = "";
    this.getInfo();
  }

  infoChange() {
    this.change = false;
  }

  //获得信息
  getInfo() {
    const http$ = this.exceptionReportApi.getExceptionReportInfo(this.planPositionId);
    http$.subscribe((data: any) => {
      if (data.state) {
        this.exceptionInfo = data.exceptionInfo;
        this.planCode = data.planCode;
        this.addressName = data.addressName;
      } else {
        this.showMsg(data.msg);
      }
    });
  }

  //异常信息上报
  report() {
    let loader = this.loadingCtrl.create({
      content: "正在提交...",
    });
    loader.present();
    const http$ = this.exceptionReportApi.exceptionReport(this.planPositionId, this.planId, this.exceptionInfo);
    http$.subscribe((data: any) => {
      if (data.state) {
        loader.dismiss();
        let alert = this.alertCtrl.create({
          title: '信息',
          subTitle: '上报成功',
          buttons: ['确认']
        });
        alert.present();
        this.goBack();
      } else {
        this.showMsg(data.msg);
      }

    });
  }


  //点击取消返回
  goBack() {
    this.navCtrl.pop();
  }


  showMsg(msg: string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }
}
